package com.zzt.mybatis.mapper;

import com.fasterxml.jackson.databind.node.IntNode;
import com.zzt.mybatis.model.UserInfo;
import com.zzt.mybatis.service.UserInfoService;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-02-27
 * Time: 14:29
 */
@Mapper
public interface UserInfoMapper {

//    @Select("select * from user_info")
//    @Select("SELECT id, username, `password`, age, gender, phone, " +
//            "delete_flag AS deleteFlag, create_time AS createTime, update_time AS updateTime FROM `user_info`")
//    List<UserInfo> selectAll();

//    @Results(id = "BaseMap", value = {
//            @Result(column = "delete_flag", property = "deleteFlag"),
//            @Result(column = "create_time", property = "createTime"),
//            @Result(column = "update_time", property = "updateTime")
//    })
//    @Select("select * from user_info")
//    List<UserInfo> selectAll();

    @Select("select * from user_info")
    List<UserInfo> selectAll();

//    @ResultMap(value = "BaseMap")
    @Select("SELECT * FROM `user_info` where id = #{id}")
    UserInfo selectAllById(Integer id);

    @Select("SELECT * FROM `user_info` where id = '${id}'")
    UserInfo selectAllById3(Integer id);

    @Select("SELECT * FROM `user_info` where id = #{id}")
    List<UserInfo> selectAllById2(Integer id);

//    @Select("select * from user_info")
//    UserInfo selectAll2();

    @Select("SELECT * FROM `user_info` where username = #{userName} and `password` = #{password}")
    List<UserInfo> selectByNameAndPassword(@Param("password")String password, @Param("userName") String username);

    @Select("SELECT * FROM `user_info` where username = '${userName}' and `password` = '${password}'")
    List<UserInfo> selectByNameAndPassword2(@Param("password")String password, @Param("userName") String username);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age) VALUES (#{username}, #{password}, #{age})")
    Integer insertUser(UserInfo userInfo);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into " +
            "user_info (username, `password`, age) " +
            "VALUES (#{userInfo.username}, #{userInfo.password}, #{userInfo.age})")
    Integer insertUser2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from user_info where id = #{id}")
    Integer deleteUser(Integer id);

    @Update("update user_info set delete_flag = #{deleteFlag}, phone = #{phone} where id = #{id}")
    Integer updateUser(UserInfo userInfo);

    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectUserInfoByOrder(String order);

    @Select("select * from user_info where username like '%${username}%'")
    List<UserInfo> selectUserInfoByLike(String username);

    @Select("select * from user_info where username like CONCAT('%', #{username}, '%')")
    List<UserInfo> selectUserInfoByLike2(String username);

    @Insert("<script>" +
            "insert into user_info"+
            "<trim prefixOverrides=\",\" suffixOverrides=\",\" prefix=\"(\" suffix=\")\">"+
            "<if test=\"username != null\"> username, </if>"+
            "<if test=\"password != null\"> `password`, </if>"+
            "<if test=\"age != null\"> age, </if>"+
            "<if test=\"gender != null\"> gender, </if>"+
            "<if test=\"phone != null\"> phone, </if>"+
            "</trim>"+
            "VALUES"+
            "<trim prefixOverrides=\",\" suffixOverrides=\",\" prefix=\"(\" suffix=\")\">"+
            "<if test=\"username != null\"> #{username}, </if>"+
            "<if test=\"password != null\"> #{password}, </if>"+
            "<if test=\"age != null\"> #{age}, </if>"+
            "<if test=\"gender != null\"> #{gender}, </if>"+
            "<if test=\"phone != null\"> #{phone}, </if>"+
            "</trim>"+
            "</script>")
    Integer insertUser3(UserInfo userInfo);
}
